
package com.jf.cloud.payment.controller.platform;

import com.jf.cloud.api.payment.vo.AccountDetailVO;
import com.jf.cloud.api.payment.vo.ShopAccountDetailVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.payment.dto.AccountSearchDTO;
import com.jf.cloud.payment.service.PayInfoService;
import com.jf.cloud.payment.service.RefundInfoService;
import com.jf.cloud.payment.vo.PayInfoVO;
import com.jf.cloud.payment.vo.RefundInfoVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author zz
 */
@RestController("platformAccountDetailController")
@RequestMapping("/p/account_detail")
@Tag(name = "platform-账户详情")
public class AccountDetailController {
    @Autowired
    private PayInfoService payInfoService;

    @Autowired
    private RefundInfoService refundInfoService;

    @GetMapping("/get_income_account_detail")
    @Operation(summary = "获取收入账户详情" , description = "根据时间获取")
    public ServerResponseEntity<AccountDetailVO> getIncomeAccountDetail(@ParameterObject AccountSearchDTO accountSearchDTO){
        if (Objects.equals(accountSearchDTO.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            AccountDetailVO platformIncomeAccountDetail = payInfoService.getPlatformIncomeAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime());
            return ServerResponseEntity.success(platformIncomeAccountDetail);
        }
        AccountDetailVO accountDetailVO = payInfoService.getIncomeAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime(), accountSearchDTO.getShopName());
        return ServerResponseEntity.success(accountDetailVO);
    }

    @GetMapping("/get_refund_account_detail")
    @Operation(summary = "获取退款账户详情" , description = "根据时间获取")
    public ServerResponseEntity<AccountDetailVO> getRefundAccountDetail(@ParameterObject AccountSearchDTO accountSearchDTO){
        if (Objects.equals(accountSearchDTO.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            AccountDetailVO platformRefundAccountDetail = refundInfoService.getPlatformRefundAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime());
            return ServerResponseEntity.success(platformRefundAccountDetail);
        }
        AccountDetailVO accountDetailVO = refundInfoService.getRefundAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime(), accountSearchDTO.getShopName());
        return ServerResponseEntity.success(accountDetailVO);
    }

    @GetMapping("/get_income_account_detail_info")
    @Operation(summary = "分页获取收入账户详情" , description = "分页获取收入账户详情")
    public ServerResponseEntity<PageVO<ShopAccountDetailVO>> getIncomeAccountDetailInfo(PageDTO pageDTO, @ParameterObject AccountSearchDTO accountSearchDTO) {
        PageVO<ShopAccountDetailVO> accountDetailPage = payInfoService.pageIncomeAccountDetail(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(accountDetailPage);
    }

    @GetMapping("/get_refund_account_detail_info")
    @Operation(summary = "分页获取退款账户详情" , description = "分页获取退款账户详情")
    public ServerResponseEntity<PageVO<ShopAccountDetailVO>> getRefundAccountDetailInfo(PageDTO pageDTO, @ParameterObject AccountSearchDTO accountSearchDTO) {
        PageVO<ShopAccountDetailVO> accountDetailPage = refundInfoService.pageRefundAccountDetail(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(accountDetailPage);
    }

    @GetMapping("/get_pay_info")
    @Operation(summary = "分页获取指定店铺的收入结算明细" , description = "根据分页参数和时间获取")
    public ServerResponseEntity<PageVO<PayInfoVO>> getPayInfo(PageDTO pageDTO, @ParameterObject AccountSearchDTO accountSearchDTO){
        PageVO<PayInfoVO> pageVO = payInfoService.getPayInfoPage(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/get_refund_info")
    @Operation(summary = "分页获取指定店铺的退款结算明细" , description = "根据分页参数和时间获取")
    public ServerResponseEntity<PageVO<RefundInfoVO>> getRefundInfo(PageDTO pageDTO, @ParameterObject AccountSearchDTO accountSearchDTO){
        PageVO<RefundInfoVO> pageVO = refundInfoService.getRefundInfoPage(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/sold_excel")
    @Operation(summary = "导出收入对账excel" , description = "导出收入对账excel")
    public ServerResponseEntity<Void> soldExcel(HttpServletResponse response, @ParameterObject AccountSearchDTO accountSearchDTO) {
        payInfoService.excelPayInfo(response, accountSearchDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/refund_sold_excel")
    @Operation(summary = "导出退款对账excel" , description = "导出退款对账excel")
    public ServerResponseEntity<Void> refundSoldExcel(HttpServletResponse response, @ParameterObject AccountSearchDTO accountSearchDTO) {
        refundInfoService.excelRefundInfo(response, accountSearchDTO);
        return ServerResponseEntity.success();
    }
}
